package com.yyfax.account.service;

import com.google.common.collect.Lists;
import com.jaws.core.common.page.Page;
import com.yyfax.account.common.enums.AccountTypeEnum;
import com.yyfax.account.common.enums.TableNameEnum;
import com.yyfax.account.common.util.TableSegmentUtils;
import com.yyfax.account.dao.AccountFlowDao;
import com.yyfax.account.dao.AccountInfoDao;
import com.yyfax.account.dao.DayChangeResultDao;
import com.yyfax.account.domain.AccountInfo;
import com.yyfax.account.vo.AccountFlowVO;
import com.yyfax.account.vo.AccountInfoVO;
import com.yyfax.account.vo.UserQueryFlowReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountQueryServiceImpl implements AccountQueryService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	// private final static String memCacheKey = "AllAccountAmountTotal";

	@Resource
	private AccountInfoDao accountInfoDao;

	@Resource
	private AccountFlowDao accountFlowDao;

	@Resource
	private DayChangeResultDao dayChangeResultDao;

	// @Resource(name = "accountCacheService")
	// private CacheService cacheService;

	@Override
	public AccountInfoVO queryAccountInfoByUser(String userId) throws Exception {
		AccountInfo po = accountInfoDao.queryAccountInfoByUser(userId);
		return buildAccountInfoVo(po);
	}

	@Override
	public List<AccountInfoVO> queryAccountInfoListByUsers(List<String> userId) throws Exception {
		return accountInfoDao.queryAccountInfoListByUsers(userId, null);
	}

	@Override
	public Page<AccountFlowVO> queryAccountFlowByUser(UserQueryFlowReq req) {
		String segment = TableSegmentUtils.getTableSegment(TableNameEnum.ACCOUNT_FLOW, req.getUserId());
		return accountFlowDao.queryAccountFlowByUser(req, req.getPageNo(), req.getPageSize(), segment).toPage();
	}

	@Override
	public AccountInfoVO queryPlatformAccount(AccountTypeEnum accountType) {
		AccountInfo po = accountInfoDao.queryPlatformAccount(accountType.getCode());
		return buildAccountInfoVo(po);
	}

	@Override
	public List<AccountInfoVO> queryAllPlatformAccount(List<String> accountTypes) {
		List<AccountInfo> list = accountInfoDao.queryAllPlatformAccount(accountTypes);

		List<AccountInfoVO> vos = Lists.newArrayList();
		for (AccountInfo po : list) {
			AccountInfoVO vo = buildAccountInfoVo(po);
			vos.add(vo);
		}

		return vos;
	}

	/**
	 * PO转VO
	 *
	 * @param po
	 * @return
	 * @author maojian
	 * @date 2017-05-24 09:51:48
	 * @since v1.0.0
	 */
	private AccountInfoVO buildAccountInfoVo(AccountInfo po) {
		if (po == null) {
			return null;
		}
		AccountInfoVO vo = new AccountInfoVO();
		vo.setAccountNo(po.getAccountNo());
		vo.setAccountName(po.getAccountName());
		vo.setDepositAccountNo(po.getDepositAccountNo());
		vo.setUserId(po.getUserId());
		vo.setAccountType(po.getAccountType());
		vo.setAmount(po.getAmount());
		vo.setFrozenAmount(po.getFrozenAmount());
		vo.setRemark(po.getRemark());
		return vo;
	}

	// /**
	// * 统计全部客户账户余额
	// *
	// * @return
	// * @author liangqm
	// * @date 2017-10-20 9:31:18
	// * @params
	// * @since v1.0.0
	// */
	// @Override
	// public AccountTotalAmountVo queryAllAccountAmountTotal() {
	// String OP = "AccountQueryServiceImpl.queryAllAccountAmountTotal";
	//
	// AccountTotalAmountVo vo = cacheService.get(memCacheKey);
	//
	// if (vo == null) {// 缓存不存在去db查询
	// vo = accountInfoDao.queryAllAccountTotalAmount();
	// vo.setAvailableAmount(vo.getAmount() - vo.getFrozenAmount());
	// boolean result = false;
	// int tryNum = 0;
	// while (!result) {
	// if (tryNum >= 3) {
	// logger.warn(Log.op(OP).msg("更新缓存连续失败三次").toString());
	// break;// 连续三次失败，直接跳出循环
	// }
	//
	// try {
	// result = cacheService.set(memCacheKey, vo, 2 * 60 * 60);// 更新缓存，2 hours 有效期
	// } catch (Exception e) {
	// logger.warn(Log.op(OP).msg("更新缓存失败").toString(), e);
	// } finally {
	// ++tryNum;
	// }
	// }
	// }
	// return vo;
	// }
}
